package com.htandroid.htandroid.webservice;

import com.htandroid.htandroid.entity.ZcpdPerson;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class abXMLZcpdPersonlist {

    /**
     * 解析评论xml信息
     *
     * @param content
     * @return
     */
    public static ArrayList<ZcpdPerson> paraseCommentInfors(String content) {
        List<String> list = new ArrayList<String>();
        content = content.replace("ZcpdPerson=anyType", "#=#");
        String[] strs = content.split("#=");
        for (int i = 0; i < strs.length; i++) {
            if (strs[i].contains("#")) {
                list.add(strs[i].replace("#", "").trim().replace(" ", ""));
            }
        }
        ArrayList<ZcpdPerson> xzpdpersonlist = new ArrayList<ZcpdPerson>();
        for (int i = 0; i < list.size(); i++) {
            if (i == list.size() - 1) {
                list.set(i, list.get(i).replace(";};};}", "\"}"));
            }
            list.set(i, list.get(i).replace(";};", "\"}").replace("=", "\":\"").replace(";", "\",\"").replace("{cuser_id", "{\"cuser_id"));
            try {
                ZcpdPerson info = new ZcpdPerson();
                JSONObject dataJson = new JSONObject(list.get(i));
                info.setCuser_id(string2null(dataJson.getString("cuser_id")));
                info.setCuser_name(string2null(dataJson.getString("cuser_name")));
                xzpdpersonlist.add(info);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return xzpdpersonlist;
    }

    //函数作用是把null 转为空
    public static String string2null(String str) {
        String rtn = "";
        if (str == null || str.equals("null") || str.equals("anyType{}")) {
            rtn = "";
        } else {
            rtn = str;
        }
        return rtn;
    }
}
